var app = getApp();
let actid, shareTitle


Page({
	data: {
		actDetail: [],
		isiJoined: 0,
		isExpire: 0,
		enrollList: [],
		userName: '',
		userTelephone: '',
		isSignDetail: 0,
	},
	onShareAppMessage: function () {
		return {
			title: shareTitle,
			desc: '重大体院研支部',
			path: 'cqdx_communist/pages/activtydetail/activtydetail/?id=' + actid,
			success: function (res) {
				wx.showToast({
					title: '转发成功',
					icon: 'none',
					duration: 2000
				})
			},  
      		fail: function (res) {  
				// 转发失败 
				wx.showToast({
					title: '转发失败',
					icon: 'none',
					duration: 2000
				}) 
			}  
		}
	},
	onLoad(obj) {
		console.log(obj)
		actid = obj.id;
		app.util.request({
			url: 'entry/wxapp/VolunteerDetail',
			data: {
				volunId: actid
			},
			success: (res) => {
				console.log(res)
				wx.setNavigationBarTitle({
					title: res.data.data.Array.title
				})
				shareTitle = res.data.data.Array.title
				this.setData({actDetail: res.data.data.Array})
				this.setData({enrollList: res.data.data.Array.enroll_list})
				this.setData({isExpire: res.data.data.Array.isExpire})
			}
		})
		//判断是否报名
		app.util.request({
			url: 'entry/wxapp/EnrollVolunteer',
			data: {
				openid: app.globalData.userInfo.openId,
				volunId: actid
			},
			success: (res) => {
				this.setData({isiJoined: res.data.data.isiJoined})
			}
		})
	},


	//自定义点击事件
	handleSign() {
		let nameReg = /^[\u4E00-\u9FA5]{2,4}$/
		if(!nameReg.test(this.data.userName)) {
			wx.showToast({
				title: '请输入正确的姓名',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		let phoneReg = /^[1][3,4,5,7,8][0-9]{9}$/;
		if (!phoneReg.test(this.data.userTelephone)) {
			wx.showToast({
				title: '请输入正确的手机号码',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		app.util.request({
			url: 'entry/wxapp/EnrollVolunteer',
			data: {
				openid: app.globalData.userInfo.openId,
				volunId: actid,
				username: this.data.userName,
				usertelephone: this.data.userTelephone,
				isiJoined: 1
			},
			success: (res) => {
				if(res.data.data.isiJoined == 1) {
					wx.showToast({
						title: '报名成功！',
						icon: 'none',
						duration: 2000
					})
					this.setData({isiJoined: res.data.data.isiJoined})
					this.hideMask()
					app.util.request({
						url: 'entry/wxapp/VolunteerDetail',
						data: {
							volunId: actid
						},
						success: (res) => {
							this.setData({actDetail: res.data.data.Array})
							this.setData({enrollList: res.data.data.Array.enroll_list})
							this.setData({isExpire: res.data.data.Array.isExpire})
						}
					})
				}
			}
		})
	},
	showMask() {
		this.setData({isSignDetail: 1})
	},
	hideMask() {
		this.setData({isSignDetail: 0})
	},
	bindKeyInputName(e) {
		this.setData({userName: e.detail.value})
	},
	bindKeyInputTelephone(e) {
		this.setData({userTelephone: e.detail.value})
	}
})